import { details, assetInfo, logout } from '@/api/user'



export default {
    state: {
        "account": "0",
        "area_code": "0",
        "phone": "0",
        "email": "0",
        "name": "0",
        "avatar": "0",
        "authentication": "0", //认证状态0未认证1初级认证2高级认证待审核3高级认证通过4高级认证拒绝
        "totalUsdt": '0', // 总折合资产 USDT
        "totalCny": "0", //总折合资产 CNY
        "assetList": {
            'BCH':{
                balance: "0",
                frost: "0",
            },
            'BTC': {
                balance: "0",
                frost: "0",
            },
            'EOS': {
                balance: "0",
                frost: "0",
            },
            'ETC': {
                balance: "0",
                frost: "0",
            },
            'ETH': {
                balance: "0",
                frost: "0",
            },
            'LTC': {
                balance: "0",
                frost: "0",
            },
            'USDT': {
                balance: "0",
                frost: "0",
            },
            'XRP': {
                balance: "0",
                frost: "0",
            },
        },
    },
    mutations: {  //mutations是操作state数据的方法的集合，比如对该数据的修改、增加、删除等等
        account(state, account) {
            state.account = account
        },
        areacode(state, areacode) {
            state.area_code = areacode
        },
        phone(state, phone) {
            state.phone = phone
        },
        email(state, email) {
            state.email = email
        },
        name(state, name) {
            state.name = name
        },
        avatar(state, avatar) {
            state.avatar = avatar
        },
        authentication(state, authentication) {
            state.authentication = authentication
        },
        totalUsdt(state, totalUsdt) {
            state.totalUsdt = totalUsdt
        },
        totalCny(state, totalCny) {
            state.totalCny = totalCny
        },
        assetList(state, assetList) {
            state.assetList = assetList
        },
    },
    getters: { //state中的成员加工后传递给外界
        // messageUnreadCount: state => state.messageUnreadList.length,
    },
    actions: {//由于直接在mutation方法中进行异步操作，将会引起数据失效。所以提供了Actions来专门进行异步操作，最终提交mutation方法
        // 退出登录
        handleLogOut({ state, commit }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('setToken', '')
                    commit('setAccess', [])
                    resolve()
                }).catch(err => {
                    reject(err)
                })
                // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
                commit('setToken', '')
                commit('setAccess', [])
                resolve()
            })
        },
        // 获取用户相关信息
        getUserInfo({ state, commit }) {
            // debugger
            return new Promise((resolve, reject) => {
                try {
                    details().then(res => {
                        const data = res.user;
                        commit('account', data.account);
                        commit('areacode', data.area_code);
                        commit('phone', data.phone);
                        commit('email', data.email);
                        commit('name', data.nickname);
                        commit('avatar', data.avatar);
                        commit('authentication', data.authentication);
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
         // 资产信息
         assetInfo({ state, commit }) {
            return new Promise((resolve, reject) => {
                try {
                    assetInfo().then(res => {
                        const data = res;
                        commit('totalUsdt', data.total_usdt);
                        commit('totalCny', data.total_cny);
                        let assetListdata = {};
                        for (let index = 0; index < data.asset_list.length; index++) {
                            assetListdata[data.asset_list[index].code] = data.asset_list[index];
                        }
                        commit('assetList', assetListdata);
                        // console.log(assetListdata)
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
    }
}
